package com.wj.opratorlog;

import com.google.common.collect.Maps;
import io.debezium.engine.ChangeEvent;
import io.debezium.engine.DebeziumEngine;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


@SpringBootApplication
@Slf4j
@EnableAsync
public class OpratorLogApplication implements CommandLineRunner {

    public static Object obj = new Object();

    public static void main(String[] args) throws FileNotFoundException {
        new SpringApplication(OpratorLogApplication.class).run(args);
         log.info("服务已启动...");


         log.info("================ssss==:"+ LocalDateTime.now());
        ;






        // OCRScaner();
//      //  test1();
//        List<Integer> integers = Arrays.asList(4444, 2, 4345, 4545, 222, 12, 23, 4, 4, 55);
//
//        quickSort(integers1 ,0,integers1.length-1)

    }

    private static void closeEngine(DebeziumEngine<ChangeEvent<String, String>> engine) {
        try {
            engine.close();

        } catch (IOException ignored) {
        }

    }

    private static void addShutdownHook(DebeziumEngine<ChangeEvent<String, String>> engine) {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> closeEngine(engine)));

    }

    private static void awaitTermination(ExecutorService executor) {
        if (executor != null) {
            try {
                executor.shutdown();

                while (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
                }

            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();

            }

        }

    }



    private static HashMap<String, Integer> getMapInfo(HashMap<String, Integer> map) {

        Optional<Map.Entry<String, Integer>> max = map.entrySet().stream().max((o1, o2) -> o1.getValue() - o2.getValue());

        max.ifPresent(item -> {

            log.info("==========最大==:" + item);
        });

        byte[] arrrrrrr = null;

        new String(arrrrrrr, StandardCharsets.UTF_8);

        Optional<Map.Entry<String, Integer>> collect = map.entrySet().stream().collect(Collectors.maxBy((o1, o2) -> o1.getValue() - o2.getValue()));


        ServiceLoader<Collection> load = ServiceLoader.load(Collection.class);

        Iterator<Collection> iterator = load.iterator();
        while (iterator.hasNext()) {
            Collection next = iterator.next();
            next.add(null);
        }
        return null;
    }


    public static void UtisDemo() {

        String adminsefefefefe = ObjectUtils.getIdentityHexString("adminsefefefefe");
        log.info("hashcode值：" + adminsefefefefe);

        List<String> list = new ArrayList<>();
        list.add("test333");
        list.add("admin333");
        String s = StringUtils.arrayToCommaDelimitedString(list.toArray());


        int i = Runtime.getRuntime().availableProcessors();
        System.out.println("处理器个数：" + i);


        log.info("逗号分隔数组：" + s);
    }

    public static void OCRScaner() throws FileNotFoundException {
        File trainData = ResourceUtils.getFile("classpath:images/chi_sim.traineddata");

       // System.setProperty( "TESSDATA_PREFIX","D:\\companyproject\\oprator-log\\src\\main\\resources\\images");

        // 识别图片的路径（修改为自己的图片路径）
//        String path = "C:\\Users\\Tang\\Desktop\\图片\\营业执照4.jpg";
        //  String path = "C:\\Users\\Tang\\Desktop\\图片\\身份证3.jpg";
//        String path = "C:\\Users\\Tang\\Desktop\\图片\\其他图片2.png";

        // 语言库位置（修改为跟自己语言库文件夹的路径）
        File idCardData = ResourceUtils.getFile("classpath:images/身份证.jpg");
        ITesseract instance = new Tesseract();
        String parent = idCardData.getParent();
        //设置训练库的位置
        instance.setDatapath(parent);

        //chi_sim ：简体中文， eng    根据需求选择语言库
        instance.setLanguage("chi_sim");

        String result = null;
        try {
            long startTime = System.currentTimeMillis();
            result = instance.doOCR(idCardData);
            long endTime = System.currentTimeMillis();
            System.out.println("Time is：" + (endTime - startTime) + " 毫秒");
        } catch (TesseractException e) {
            e.printStackTrace();
        }

        System.out.println("result: ");
        System.out.println(result);
    }


  static    void  test1(){
        String  name="1";
        String name2= new String("1");

        String ssss= new String("1")+new String("1");
        System.out.println(name==name2);
        System.out.println(name.equals(name2));
        System.out.println(name.hashCode());
        System.out.println(name2.hashCode());

        name.intern();
      ssss.intern();

      System.out.println(ssss=="11");

    }


    static   void  quickSort( int[] arr, int low ,int hight) {
        if (low<hight){
            int p = partion(arr ,low, hight);
            quickSort(  arr, low,p-1);
            quickSort(arr,p+1,hight);
        }

    }
    static  int partion(  int[] arr ,int low  ,int hight){


         Integer p = arr[0];
        while (low<hight){
            while (low<hight&& arr[hight]>p){
                hight--;
            }
            while (low<hight&& arr[low]<p){
                low++;
            }
            if (low<hight){
                int temp =arr[low];
                arr[low]=arr[hight];

            }

        }
      arr[low]=p;

        return  low;
    }

    @Override
    public void run(String... args) throws Exception {



    }
}
